package com.ramnight.sql2.ext.name;

public class NameUtil {

	public static String firstCharLower(String name) {
		String firstChar = name.substring(0, 1);
		return name.replaceFirst(firstChar, firstChar.toLowerCase());
	}
	
	public static String[] camel2underline(String[] names) {
		return camel2underline(names, false);
	}

	public static String[] camel2underline(String[] names, boolean ignoreFirstCapital) {
		for(int i=0; i<names.length; i++){
			names[i] = camel2underline(names[i], ignoreFirstCapital);
		}
		return names;
	}
	
	public static String camel2underline(String name) {
		return camel2underline(name, false);
	}

	public static String camel2underline(String name, boolean ignoreFirstCapital) {
		StringBuilder newName = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			if (Character.isUpperCase(c)) {// 忽略第一个大写
				if(i == 0 && ignoreFirstCapital){
					newName.append(Character.toLowerCase(c));
				}else{
					newName.append('_').append(Character.toLowerCase(c));
				}
			}else{
				newName.append(c);
			}
		}
		return newName.toString();
	}
	
	public static String[] underline2camel(String[] names){
		return underline2camel(names, false);
	}
	
	public static String[] underline2camel(String[] names, boolean firstUnderlineToUpper){
		for(int i=0; i<names.length; i++){
			names[i] = underline2camel(names[i], firstUnderlineToUpper);
		}
		return names;
	}
	
	public static String underline2camel(String name){
		return underline2camel(name, false);
	}
	
	public static String underline2camel(String name, boolean firstUnderlineToUpper){
		StringBuilder newName = new StringBuilder();
		boolean needUpper = false;
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			if (c == '_') {
				if(i == 0 && !firstUnderlineToUpper){
					newName.append(c);
				}else{
					needUpper = true;
				}
			}else{
				if(needUpper){
					newName.append(Character.toUpperCase(c));
					needUpper = false;
				}else{
					newName.append(c);
				}
			}
		}
		return newName.toString();
	}
}
